<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小红书评论</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .comment {
            border: 1px solid #ccc;
            padding: 10px;
            margin: 10px 0;
        }
        .comment .author {
            font-weight: bold;
        }
        .comment .content {
            margin-top: 5px;
        }
        .comment .created-at {
            font-size: 0.8em;
            color: #666;
        }
        form {
            margin-top: 20px;
        }
        input, textarea {
            display: block;
            width: 100%;
            margin-bottom: 10px;
            padding: 10px;
        }
        button {
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<h1>小红书评论</h1>
<div id="comments"></div>
<form id="commentForm">
    <input type="text" id="author" placeholder="你的名字" required>
    <textarea id="content" placeholder="你的评论" required></textarea>
    <button type="submit">提交评论</button>
</form>

<script>
    async function fetchComments() {
        const response = await fetch('/commentai');
        const comments = await response.json();
        const commentsContainer = document.getElementById('comments');
        commentsContainer.innerHTML = '';
        comments.forEach(comment => {
            const commentElement = document.createElement('div');
            commentElement.className = 'comment';
            commentElement.innerHTML = `
                    <div class="author">${comment.author}</div>
                    <div class="content">${comment.content}</div>
                    <div class="created-at">${new Date(comment.createdAt).toLocaleString()}</div>
                `;
            commentsContainer.appendChild(commentElement);
        });
    }

    document.getElementById('commentForm').addEventListener('submit', async (event) => {
        event.preventDefault();
        const author = document.getElementById('author').value;
        const content = document.getElementById('content').value;
        const response = await fetch('/commentai', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ author, content, createdAt: new Date() })
        });
        if (response.ok) {
            document.getElementById('author').value = '';
            document.getElementById('content').value = '';
            fetchComments();
        }
    });

    fetchComments();
</script>
</body>
</html>
